// 路径拼接
export const pathResolve = (parentPath: string, path: string) => {
  const childrenPath = path.startsWith('/') || !path ? path : `/${path}`
  return `${parentPath}${childrenPath}`.replace(/\/\//g, '/').trim()
}

// 获取view下所有组件
const modules = import.meta.glob("@/views/**/*.vue")

export const Layout = () => import("@/layout/Layout.vue")
export const NotFoundComponent = () => import("@/views/admin/Error/NotFoundComponent.vue")

export const getParentLayout = () => {
  return () =>
    new Promise((resolve) => {
      resolve({
        name: 'ParentLayout'
      })
    })
}
// 生成路由
export const generateRouter = (routes: any) => {

  const ret = []
  for (const route of routes) {
    const data: any = {
      path: route.path,
      name: route.name,
      redirect: route.redirect,
      meta: route.meta,
    }
    if (route.component) {
      // console.log(modules);
      const comMod = modules[`/src/views${route.component}.vue`] || modules[`/src/views${route.component}.tsx`]
      const component = route.component
      if (!comMod && !component.includes('#')) {
        data.component = NotFoundComponent
        console.error(`未找到组件${component}`)
      } else {
        data.component = component.includes("#") ? Layout : component.includes("##") ? null : comMod
        // data.component = component.includes("#") ? Layout : component.includes("##") ? Layout : comMod
      }
    }

    // 如果该路由为顶级菜单，并且菜单类型为菜单


    if (route.children) {
      data.children = generateRouter(route.children)
    }

    // 如果该路由为顶级菜单，并且菜单类型为菜单
    if (route.meta.menuType === "menu" && route.parentId === null) {
      // console.log(111, route);
      const comMod = modules[`/src/views${route.component}.vue`] || modules[`/src/views${route.component}.tsx`]
      const layoutComponent = {
        path: "/",
        component: Layout,
        meta: {},
        children: [
          {
            path: route.path,
            name: route.name,
            component: comMod,
            meta: route.meta,
            redirect: route.redirect,
          }
        ]
      }
      ret.push(layoutComponent)
    } else {
      ret.push(data)
    }

  }
  return ret
}

// 获取角色菜单的第一个菜单
export const getFirstMenuUrl = (menus: any): string => {
  let firstMenuUrl = "";
  // console.log(menus);

  const findFirstMenu = (menus: any, path: string) => {
    return menus.find((item: any) => {
      if (item.meta.menuType === "menu" && item.meta.hidden != 1) {
        firstMenuUrl = pathResolve(path, item.path)
        return true
      } else if (item.children && item.children.length > 0 && item.meta.hidden != 1) {
        path = pathResolve(path, item.path)
        if (findFirstMenu(item.children, path)) {
          return true;
        } else {
          path = "";
          firstMenuUrl = ""
          return false
        }
      }
    })
  }

  if (findFirstMenu(menus, "")) {
    return firstMenuUrl;
  } else {
    return ""
  }

}


